import router from "@/router"
import nprogress from "nprogress"
import "nprogress/nprogress.css"
import store from "@/store"
import userUserStore from "@/store/modules/user"
import path from "path"
import setting from "./setting"
import { nextTick } from "process"
const userStore = userUserStore(store)
console.log(userStore.token);
const token = userStore.token;
router.beforeEach(async (to, from, next) => {
  document.title = setting.logo_title + to.meta.title
  nprogress.start();
  console.log(to);
  const username = userStore.username
  if (token && localStorage.getItem("token")) {
    console.log("有token");
    if (to.path != '/login') {
      // 来到主页
      if (!username) {
        try {
          console.log("执行获得用户信息");
          await userStore.requestUserInfo()
          next({ ...to })
        } catch (error) {
          console.log(error);
          userStore.logout()
          next({ path: "/login" })
        }
      } else {
        // 有用户信息
        next()
      }
    } else {
      next({ path: "/" })
    }
  } else {
    console.log("无token");
    // 没有登录
    if (to.path == '/login') {
      next()
    } else {
      next({ path: "/login" })
    }
  }
  // 放行函数
})

// 全局后置守卫
router.afterEach((to, from) => {
  nprogress.done();
});



